//
//  BeLostViewController.m
//  PetAPP
//
//  Created by dllo on 16/1/29.
//  Copyright © 2016年 Yuming. All rights reserved.
//

#import "BeLostViewController.h"
#import "NetworkingHandle.h"
#import <MJRefresh.h>
#import "ModelOfBeLostBanner.h"
#import "ModelOfBeLostDateList.h"
#import "CellOfBeLost.h"
#import <YYWebImage.h>
#import "BeLostBannerVC.h"
#import "BeLostHelpVC.h"
#import "BeLostSelectFindPetVC.h"


@interface BeLostViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, assign) NSInteger refreshNum;

@property (nonatomic, retain) ModelOfBeLostBanner *modelOfBanner;

@property (nonatomic, retain) NSMutableArray *dataArr;

@property (nonatomic, retain) UIImageView *viewOfBanner;


@end

@implementation BeLostViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self handleData];
    [self createTableViewSubView];
    
    [self changeNavigationBar];
    [self handleRefresh];
    [self findMyPet];
    
    
}

- (void)handleFindPet:(UIButton *)button {
    BeLostSelectFindPetVC *select = [[BeLostSelectFindPetVC alloc] init];
    
    [self.navigationController pushViewController:select animated:YES];
    
    [select release];
}

- (void)findMyPet {

    UIButton *buttonOfFind = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonOfFind.frame = CGRectMake(0, 0, 70, 70);
    
    buttonOfFind.center = CGPointMake(self.view.center.x, self.view.frame.size.height - 60);
    [self.view addSubview:buttonOfFind];
    
    buttonOfFind.layer.cornerRadius = 35;
    [buttonOfFind setTitle:@"我要寻宠" forState:UIControlStateNormal];
    buttonOfFind.titleLabel.font = [UIFont systemFontOfSize:16];
    [buttonOfFind setTitleColor:[UIColor colorWithRed:0.6 green:0.4 blue:0.2 alpha:1.0]forState:UIControlStateNormal];
    buttonOfFind.backgroundColor = [UIColor colorWithRed:1.0 green:0.8423 blue:0.2839 alpha:1.0];
    [buttonOfFind addTarget:self action:@selector(handleFindPet:) forControlEvents:UIControlEventTouchUpInside];
    
    

}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.refreshNum = 1;
        [self handleData];
    }];

}


- (void)changeNavigationBar {
    self.navigationItem.title = @"防走失平台";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor brownColor],
                                                                    NSFontAttributeName : [UIFont systemFontOfSize:22]};
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 40, 40);
    [button setTitle:@"帮助" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:18];
    [button setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    [button addTarget:self action:@selector(helpHandleAction:) forControlEvents:UIControlEventTouchUpInside];
    
}


- (void)handleData {
    
    NSString *body = [NSString stringWithFormat:@"lat=38.88961325&lng=121.55114556&page=1&pagesize=%ld0&uid=1411934&version=d1bd83a33f1a841ab7fda32449746cc4",self.refreshNum];
    /** 处理数据 */
    [NetworkingHandle handleWorkData:[self.URLDic objectForKey:@"url"] AndBody:body value:[self.URLDic objectForKey:@"value"] handleBlock:^(id result) {
        /** 初始化装载内容的数组 */
        self.dataArr = [NSMutableArray new];
        NSDictionary *tempDic = [result objectForKey:@"resList"];
        /** 初始化并给头部广告的model赋值 */
        _modelOfBanner = [[ModelOfBeLostBanner alloc] init];
        [_modelOfBanner setValuesForKeysWithDictionary:[tempDic objectForKey:@"banner"][0]];
        
        NSArray *tempArr = [tempDic objectForKey:@"dataList"];
        
        for (NSDictionary *dic in tempArr) {
            ModelOfBeLostDateList *model = [[ModelOfBeLostDateList alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            
            [self.dataArr addObject:model];
            [model release];
        }
        
        
        
        [self createBannerView];
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    }];
}

- (void)handleRefresh {
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.refreshNum = 1;
        [self handleData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.refreshNum++;
        [self handleData];
    }];
}


- (void)createTableViewSubView {
    self.refreshNum = 1;
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    [self.tableView registerClass:[CellOfBeLost class] forCellReuseIdentifier:@"poolOfCell"];
    
    
}

- (void)createBannerView {
    self.viewOfBanner = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
    self.viewOfBanner.yy_imageURL = [NSURL URLWithString:self.modelOfBanner.coverpath];
    self.tableView.tableHeaderView = self.viewOfBanner;
    [_viewOfBanner release];
    
    self.viewOfBanner.userInteractionEnabled = YES;
        
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bannerHandleAction:)];
    
    [self.viewOfBanner addGestureRecognizer:tap];
    

}

- (void)bannerHandleAction:(UITapGestureRecognizer *)tap {
    BeLostBannerVC *banner = [[BeLostBannerVC alloc] init];
    banner.bannerTitle = self.modelOfBanner.title;
    banner.link = self.modelOfBanner.link;
    
    [self.navigationController pushViewController:banner animated:YES];
    [banner release];
}

- (void)helpHandleAction:(UIButton *)button {
    BeLostHelpVC *help = [[BeLostHelpVC alloc] init];
    
    [self.navigationController pushViewController:help animated:YES];
    [help release];
    


}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CellOfBeLost *cell = [tableView dequeueReusableCellWithIdentifier:@"poolOfCell"];
    ModelOfBeLostDateList *model = [self.dataArr objectAtIndex:indexPath.row];
    
    [cell passContentWithModel:model];
    
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 100;
}

- (void)dealloc {
    [_tableView release];
    [_modelOfBanner release];
    [_dataArr release];
    [_viewOfBanner release];
    [_URLDic release];
    [super dealloc];

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
